{{ $hasToc := and (in .TableOfContents "<li>" ) (.Params.toc) }}
{{ $hasSidebar := or ($hasToc) (.Params.series) }}
<div class="lg:pt-12">
    <div class="flex flex-col md:flex-row bg-secondary-bg rounded">
        <div class="md:w-1/4 lg:w-1/5 border-r">
            <div class="sticky top-16 pt-6">
                {{ template "doc-sidebar" (dict "context" . "permalink" .Permalink) }}
            </div>

        </div>
        <div class="w-full md:w-3/4 lg:w-4/5 pb-8 pt-2 md:pt-8">
            <div class="w-full lg:w-3/4 pl-6 ml-0 mr-auto">
                <h1 class="font-bold text-3xl text-primary-text">{{ .Title }}</h1>
                {{ partial "components/post-metadata" . }}
            </div>
            <div class="flex">
                <div class="w-full lg:w-3/4 px-6">
                    {{ $featured := partial "utils/get-featured" . }}
                    {{ with $featured }}
                    <div class="my-4">
                        {{ . }}
                    </div>
                    {{ end }}
                    <div class="content">
                        {{ .Content}}
                    </div>
                    {{ with .GetTerms "tags" }}
                    {{ partial "components/post-tags" . }}
                    {{ end }}
                    {{ with .GetTerms "authors" }}
                    {{ partial "components/post-author" . }}
                    {{ end }}

                    {{ partial "components/post-edit" . }}

                    {{ partial "components/post-footer" . }}
                    {{ partial "comment" . }}
                </div>
                {{ if $hasSidebar}}
                <div class="hidden lg:block lg:w-1/4">
                    {{ if ne $hasToc false }}
                    {{ partial "components/post-toc" . }}
                    {{ end }}
                </div>
                {{ end }}
            </div>

        </div>


    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', () => {
        hljs.initHighlightingOnLoad();
        changeSidebarHeight();
        switchDocToc();
    })
</script>

{{ define "doc-sidebar" }}
{{/*  Deprecation warning(v1.0.0) starts */}}
{{ $parentLayout := partial "utils/hyphenate" (.context.Parent.Layout | default .context.Parent.Type) }}
{{/*  Deprecation warning(v1.0.0) ends */}}
{{ if eq $parentLayout "doc-list" }}
{{ template "list-pages" (dict "context" .context "permalink" .permalink) }}
{{ else if (findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?/$" .context.Permalink) }}
{{ template "list-pages" (dict "context" .context "permalink" .permalink) }}
{{ else }}
{{ template "doc-sidebar" (dict "context" .context.Parent "permalink" .permalink) }}
{{ end }}
{{ end }}

{{ define "list-pages" }}
<div id="sidebar-title" class="md:hidden mx-4 px-2 pt-4 pb-2 md:border-b text-tertiary-text md:text-primary-text">
    <span class="font-semibold">{{ i18n "toc" }}</span>
    <i class="fas fa-caret-right ml-1"></i>
</div>

<div id="sidebar-toc"
    class="hidden md:block overflow-y-auto mx-6 md:mx-0 pr-6 pt-2 md:max-h-doc-sidebar bg-primary-bg md:bg-transparent">
    <div class="flex flex-wrap ml-4 -mr-2 p-2 bg-secondary-bg md:bg-primary-bg rounded">
        <a class="{{ if eq .context.Permalink .permalink }}text-eureka{{ end }} hover:text-eureka"
            href="{{ .context.Permalink }}">{{ .context.LinkTitle }}</a>
        
        {{ $versions := partial "get-versions" (dict "context" .context) }}
        {{ if $versions }}
        <div class="relative ml-2 cursor-pointer">
            <div id="doc-version" class="flex items-center bg-primary-bg md:bg-secondary-bg">
                {{ $version := findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?" .context.Permalink }}
                <span class="ml-2 mr-1">{{ cond (gt (len $version) 0) (index $version 0) "latest" }}</span>
                <i class="fas fa-caret-right w-4 h-4 mr-1"></i>
            </div>
            <div id="version-options" class="hidden absolute left-0 bg-secondary-bg rounded z-40">
                {{ range $index, $value := $versions }}
                {{ if eq $index 0 }}
                <div class="ml-2 mr-8 my-1">
                    <a class="" href="{{ $.context.Parent.Permalink }}">latest</a>
                </div>
                {{ end }}
                {{ $version := findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?" . }}
                <div class="ml-2 mr-8 my-1">
                    <a class="" href="{{ . }}">{{ index $version 0 }}</a>
                </div>
                {{ end }}
            </div>
        </div>
        <div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-0" id="switch-canvas">
        </div>
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                switchDocVersion();
            })
        </script>
        {{ end }}


    </div>
    {{ template "list-items" (dict "context" .context "permalink" .permalink) }}
</div>
{{ end }}

{{ define "list-items"}}
<ul class="pl-6">
    {{ range .context.Pages }}
    {{ $version := findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?/$" .Permalink }}
    {{ if eq (len $version) 0 }}
    <li class="py-2">
        <div class="{{ if .IsSection }} pb-2 {{ end }}">
            <a class="{{ if eq .Permalink $.permalink }} text-eureka {{ end }} hover:text-eureka"
                href="{{ .Permalink }}">{{ .LinkTitle }}</a>
        </div>
        {{ if .IsSection }}
        {{ template "list-items" (dict "context" . "permalink" $.permalink) }}
        {{ end }}
    </li>
    {{ end }}
    {{ end }}
</ul>
{{ end }}

{{ define "partials/get-versions" }}
{{/*  Deprecation warning(v1.0.0) starts */}}
{{ $parentLayout := partial "utils/hyphenate" (.context.Parent.Layout | default .context.Parent.Type) }}
{{/*  Deprecation warning(v1.0.0) ends */}}
{{ if eq $parentLayout "doc-list" }}
{{ range .context.Pages.Reverse }}
{{ $hasVersion := findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?/$" .Permalink }}
{{ if $hasVersion}}
{{ $version := slice .Permalink }}
{{ $.context.Scratch.Add "versions" $version }}
{{ end }}
{{ end }}
{{ else }}
{{ range .context.Parent.Pages.Reverse }}
{{ $hasVersion := findRE "v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?/$" .Permalink }}
{{ if $hasVersion }}
{{ $version := slice .Permalink }}
{{ $.context.Scratch.Add "versions" $version }}
{{ end }}
{{ end }}
{{ end }}
{{ $versions := .context.Scratch.Get "versions" }}
{{ .context.Scratch.Delete "versions" }}
{{ return $versions }}
{{ end }}